import { apiService } from './api';
import type { FileInfo } from '../dataType/fileType';
import type { PageResponse } from '../dataType/pageType';

export const fileService = {
  getFiles: (page: number, pageSize: number): Promise<PageResponse<FileInfo>> => apiService.get<PageResponse<FileInfo>>('/api/file/listPage', {params: { page, pageSize } }),
  uploadFile: (file: File) => {
    const formData = new FormData();
    formData.append('file', file);
    return apiService.uploadFile('/api/file/upload', formData);
  },
  downloadFile: (fileId: string, fileName: string): Promise<void> => {
    return apiService.get('/api/file/download/' + fileId, { responseType: 'blob' })
      .then(response => { 
        const url = window.URL.createObjectURL(response as Blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
        document.body.removeChild(a);
      });
  }
}